// components/mPop/mLoginPop/mLoginPop.js
import request from '../../../utils/request'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    showModal: false,
    showBtn: false,
    userInfo: {}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 显示一键获取手机号弹窗
    showDialogBtn: function () {
      request.authorizeGetOpenid();
      let self = this;
      this.setData({
        showModal: true//修改弹窗状态为true，即显示
      })
      wx.getUserInfo({
        lang: 'zh_CN',
        timeout:10000,
        success: (result)=>{
          if(result.errMsg == 'getUserInfo:ok'){
            // 成功获取登陆信息
            let userInfo = result.userInfo;
            self.setData({
              userInfo: userInfo,
              showBtn: false
            });
            wx.setStorageSync('userInfo', JSON.stringify(userInfo));
          }else{
            self.setData({
              showBtn: true
            });
          }
        },
        fail: ()=>{
          self.setData({
            showBtn: true
          });
        },
        complete: ()=>{
        }
      });
    },
    // 隐藏一键获取手机号弹窗
    hideModal: function () {
      this.setData({
        showModal: false//修改弹窗状态为false,即隐藏
      });
    },
    bindGetUserInfo: function (e) {
      let self = this;
      let userInfo = e.detail.userInfo;
      self.setData({
        userInfo: userInfo,
        showBtn: false
      });
      wx.setStorageSync('userInfo', JSON.stringify(userInfo));
    },
    getPhoneNumber: function (e) {
      if(this.data.userInfo == {}){
        wx.showToast({
          title: '请先点击微信授权登陆按钮！',
          icon: 'none',
          image: '',
          duration: 1500,
          mask: false
        });
        return false;
      }
      var self = this;
      var ivObj = e.detail.iv
      var telObj = e.detail.encryptedData
      // 获取openid
      request.authorizeGetOpenid()
      .then(res => {
        let openid = res.openid;
        let data1 = {
          conid: openid
        }
        request.authorizeGetToken(data1)
        .then(function (res1) {
  
          if (res1.errcode == '0') {
            // 成功获取token
            // 通过验证
            wx.showToast({
              title: '登陆成功！',
              duration: 1500
            });
            self.hideModal();
            // 保存token
            // wx.setStorage({
            //   key: 'token',
            //   data: res1.access_token
            // });
            wx.setStorageSync('token', res1.access_token);
            // // 过期时间
            // wx.setStorage({
            //   key: 'expires_at',
            //   data: res1.profile.expires_at
            // });
            wx.setStorageSync('expires_at', res1.profile.expires_at);
            // wx.setStorage({
            //   key: 'profile',
            //   data: JSON.stringify(res1.profile)
            // });
            wx.setStorageSync('profile', JSON.stringify(res1.profile));
            self.triggerEvent('loginSuccess',res1.access_token);
          } else {
            let data = {
              encryptedData: telObj,
              iv: ivObj
            }
            request.authorizeGetPhoneNumber(data)
              .then(res => {
                self.hideModal();
                if (res.phoneNumber) {
                  // 绑定手机号
                  wx.setStorage('tel', res.phoneNumber);
                  self.setData({
                    tel: res.phoneNumber
                  });
                  self.authorizeSetConsumer();
                } else {
                  wx.showToast({
                    title: '获取手机号失败，请重新登录',
                    icon: 'none',
                    image: '',
                    duration: 1500,
                    mask: false,
                  });
                }
              });
          }
        });
      });
  
    },
    // 验证码登录（可获取token）
    authorizeSetConsumer: function () {
      let userInfo = this.data.userInfo;
      let self = this;
      // 获取openid
      request.authorizeGetOpenid()
        .then(res => {
          let openid = res.openid;
          let data = {
            conid: openid,
            contel: self.data.tel,
            conname: userInfo.nickName,
            conimage: userInfo.avatarUrl,
          }
          request.authorizeSetConsumer(data)
            .then(res2 => {
              if (res2.errcode == '0') {
                wx.showToast({
                  title: '登陆成功！',
                  duration: 1500
                });
                self.hideModal();
                // 通过验证
                // 保存token
                // wx.setStorage({
                //   key: 'token',
                //   data: res2.access_token
                // });
                wx.setStorageSync('token', res2.access_token);
                // // 过期时间
                // wx.setStorage({
                //   key: 'expires_at',
                //   data: res2.profile.expires_at
                // });
                wx.setStorageSync('expires_at', res2.profile.expires_at);
                // wx.setStorage({
                //   key: 'profile',
                //   data: JSON.stringify(res2.profile)
                // });
                wx.setStorageSync('profile', JSON.stringify(res2.profile));
                self.triggerEvent('loginSuccess',res2.access_token);
              }
            });
        });
    },
  }
})
